using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEngine.UIElements;

namespace Sailfish
{

    public enum Obsolete
    { 
        None = 0,
        Expired = 1,
        failure = 2
    }

    [FilePath("ProjectSettings/UIActionModuleData.asset", FilePathAttribute.Location.ProjectFolder)]
    public class UIActionModuleData : UnityEditor.ScriptableSingleton<UIActionModuleData>
    {
        public string saveDate;
        public List<PanelData> datas = new List<PanelData>();

        public void Apply()
        {
            base.Save(true);
        }
    }

    [System.Serializable]
    public class PanelData
    {
        public string scriptCS;
        public List<ArgsData> args = new List<ArgsData>();


        [System.Serializable]
        public class ArgsData
        {
            public Obsolete obsolete = Obsolete.None;
            public string   callArg;
            public string   fixedArg;
            public string   newFixedArg;
        }

        public class SimpleRecord
        {
            public string cmd;
            public string main;
        }
    }

    class UIActionModuleProvider : SettingsProvider
    {
        SerializedObject   m_SerializedObject;
        SerializedProperty m_datas;
        public UIActionModuleProvider(string path, SettingsScope scope = SettingsScope.User) : base(path, scope) { }

        public override void OnActivate(string searchContext, VisualElement rootElement)
        {
            UIActionModuleData.instance.Apply();
            m_SerializedObject = new SerializedObject(UIActionModuleData.instance);
            m_datas            = m_SerializedObject.FindProperty("datas");
        }

        public override void OnGUI(string searchContext)
        {
            base.OnGUI(searchContext);

            EditorGUILayout.LabelField("Record", EditorStyles.boldLabel);

            EditorGUILayout.LabelField("Update Time : " + UIActionModuleData.instance.saveDate, EditorStyles.boldLabel);

            GUILayout.Space(10);


            EditorGUILayout.PropertyField(m_datas);

            GUI.enabled = true;


            if (GUILayout.Button("再次检测数据"))
            {
                UIActionModuleEditor.AllScriptsReloaded();
                AssetDatabase.Refresh();
            }
        }

        [SettingsProvider]
        public static SettingsProvider CreateMyCustomSettingsProvider()
        {
            UIActionModuleData.instance.Apply();
            var provider = new UIActionModuleProvider("Project/Sailfish/UIActionModule", SettingsScope.Project);
            provider.keywords = GetSearchKeywordsFromGUIContentProperties<GUIContent>();
            return provider;
        }
    }

}
